define(function (require, exports, module) {
    require("apps/easyui");
    require("apps/file");

    exports.components = {
        FIELD_TYPE_TIME: function (ctx, config, value) {
            var cop = $('<input type="text" data-id="f-component" class="easyui-validatebox hydra-input-width" autocomplete="off" value=""/>');
            $(this).replaceWith(cop);
            var dataOptions = "validType:[\'length[0,50]\']";
            cop.attr("name", config.name).attr("placeholder", config.remark).attr("data-options", dataOptions)
                .datetimebox({
                    required: config.notNull ? true : false
                });
            if (value != undefined) {
                if (/\d{4}(-\d\d){2}(\S\d{2}(:\d\d){2})?/.test(value)) {
                    cop.datetimebox("setValue", value);
                } else {
                    cop.datetimebox("setValue", "");
                }
            }
            return cop;
        },
        FIELD_TYPE_TEXTAREA: function (ctx, config, value) {
            var cop = $('<textarea data-id="f-component" class="easyui-validatebox hydra-input-width hydra-textarea"></textarea>');
            $(this).replaceWith(cop);
            var dataOptions = "validType:[\'length[0,2000]\']";
            cop.attr("name", config.name).attr("data-options", dataOptions).validatebox({
                required: config.notNull ? true : false
            });
            if (value != undefined) {
                cop.val(value);
            }
            return cop;
        },
        FIELD_TYPE_TEXT: function (ctx, config, value) {
            var cop = $('<input data-id="f-component" class="easyui-validatebox hydra-input-width" type="text" autocomplete="off" value=""/>');
            $(this).replaceWith(cop);
            var dataOptions = "validType:[\'length[0,200]\']";
            cop.attr("name", config.name).attr("data-options", dataOptions).validatebox({
                required: config.notNull
            });
            if (value != undefined) {
                cop.val(value);
            }
            return cop;
        },
        FIELD_TYPE_NUM: function (ctx, config, value) {
            var cop = $('<input data-id="f-component" class="easyui-validatebox hydra-input-width" type="text" autocomplete="off" value=""/>');
            $(this).replaceWith(cop);
            var dataOptions = "validType:[\'length[0,50]\']";
            cop.attr("name", config.name).attr("data-options", dataOptions).numberbox({
                precision: -1,
                required: config.notNull ? true : false
            });
            if (value != undefined) {
                if (/[\+\-]?\d+.?\d*/.test(value)) {
                    cop.numberbox("setValue", value);
                } else {
                    cop.numberbox("setValue", "");
                }
            }
            return cop;
        },
        FIELD_TYPE_ATTACHMENT: function (ctx, config, value) {
            var cop = $('<input data-id="f-component" class="easyui-validatebox hydra-input-width" type="text" autocomplete="off" value=""/>');
            $(this).replaceWith(cop);
            cop.before('<div style="border: 1px dotted #cccccc;display: none;padding: 5px;margin-bottom: 5px">');
            if (config.notNull) {
                cop.attr("data-require", 1);
            }
            cop.attr("name", config.name).val(value == undefined ? "" : value).fileupload({
                uploader: ctx + '/app/api/file/uploadTempFile',
                onUploadSuccess: function (files) {
                    var file = files[0];
                    this.element.attr("data-name", file.name)
                        .val(file.href).prev().show().html(file.fileHtml).children().children().css({border: "none"});
                }
            });
            if (value && value.lastIndexOf(".") > -1) {
                cop.fileupload("addFile", "", value);
            }
            return cop;
        },//科学计数法
        FIELD_TYPE_SCIENTIFIC: function (ctx, config, value) {
            var cop = $('<span>实数：</span><input data-id="f-component" style="width: 50px;margin-right: 10px;" class="easyui-validatebox hydra-input-width" type="text" autocomplete="off" value=""/>' +
                '<span class="ml10">指数：</span><input data-id="f-component" style="width: 50px;" class="easyui-validatebox hydra-input-width" type="text" autocomplete="off" value=""/>');
            $(this).replaceWith(cop);
            var dataOptions = "validType:[\'length[0,100]\']";
            value = value || "";
            var test = /[\+\-]?\d+,[\+\-]?\d+/.test(value);
            var vv = value.split(",");
            var j = 0;
            cop.each(function (i, item) {
                if ($(this).attr("data-id") == "f-component") {
                    $(this).attr("name", config.name).attr("data-options", dataOptions).numberbox({
                        precision: -1,
                        required: config.notNull ? true : false
                    });
                    if (test) {
                        $(this).numberbox("setValue", vv[j]);
                    } else {
                        $(this).numberbox("setValue", "");
                    }
                    j++;
                }
            });
            return cop;
        },
        FIELD_TYPE_DIRECTORY: function (ctx, config, value) {
            var cop = $('<select data-id="f-component" class="easyui-validatebox hydra-input-width"></select>');
            $(this).replaceWith(cop);
            cop.attr("name", config.name);
            $.getJSON(ctx + "/app/api/directory/list", {dirGroup: config.fieldTypeParam}).done(function (data) {
                var content = data.content;
                cop.combobox({
                    valueField: 'dirValue',
                    textField: 'dirValue',
                    editable: !config.notNull,
                    data: content,
                    required: config.notNull ? true : false
                });
                if (value) {
                    cop.combobox("select", value)
                }
            });
            return cop;
        },
        FIELD_TYPE_TABLE: function (ctx, config, value) {
            var cop = value ? $(value) : $(config.fieldTypeParam);
            $(this).replaceWith(cop);
            cop.attr("name", config.name).attr("data-id", "f-component").addClass("normal-table");
            return cop;
        }
    }

    var imgsTypes = ["jpg","jpeg", "gif", "png", "bmp"];
    var renderMethods = {
        FIELD_TYPE_ATTACHMENT: function (ctx, v) {
            if (v && v.length && v.lastIndexOf(".") > 0) {
                var vv = v.substr(v.lastIndexOf(".") + 1);
                var p = ctx + '/download/' + v;
                if (imgsTypes.contains(vv.toLocaleLowerCase())) {
                    p = ctx + '/print/' + v;
                    return "<a href=\"" + p + "\" data-id=\"facybox\"><img style=\"width:60px;height:30px;\" src=\"" + p + "\"></a>";
                }
            }
            return v && v.length ? '<a href="' + ctx + '/download/' + v + '">附件下载</a>' : "";
        },
        FIELD_TYPE_SCIENTIFIC: function (ctx, v) {
            if (v && v.length) {
                var vv = v.split(",");
                if (vv.length == 2) {
                    return vv[0] + "×10&sup" + vv[1] + ";";
                } else {
                    return vv[0] + "×10&sup" + 0 + ";";
                }
            }
            return "";
        }
    };
    exports.render = function(ctx, type, holder){
        if(renderMethods[type]){
            $(holder).html(renderMethods[type](ctx, $(holder).html()));
            if(type == 'FIELD_TYPE_ATTACHMENT'){
                $(holder).find("[data-id=facybox]").each(function(){
                    $(this).fancybox();
                })
            }
        }
    }
});